﻿using Juli.Mes.Products.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Juli.Mes.Products;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Microsoft.AspNetCore.Identity;
using Lion.AbpPro.Core;
using Volo.Abp.Identity;

namespace Juli.Mes.Products
{
    [Route("products")]
    //[Authorize]
    public class ProductAppService : MesAppService, IProductAppService
    {
        private readonly IRepository<Product> productRepository;
        private readonly IRepository<Volo.Abp.Identity.IdentityUser> userRepository;
  
        public ProductAppService(IRepository<Product> productRepository,
            IRepository<Volo.Abp.Identity.IdentityUser> userRepository)
        {
            this.productRepository = productRepository;
            this.userRepository = userRepository;
        }

        [HttpPost("create")]
        [SwaggerOperation("创建产品", Tags = new[] { "Products" })]
        public async Task CreateProduct(ProductInputDto input)
        {
            var entity = new Product()
            {
                ProductName = input.ProductName,
                ProductCode = input.ProductCode,
                Description = input.Description,
            };
           await  productRepository.InsertAsync(entity);
        }

        [HttpPost("delete")]
        [SwaggerOperation("删除产品", Tags = new[] { "Products" })]
        public async Task DeleteProduct(IdInput input)
        {
            await productRepository.DeleteAsync(p=>p.Id==input.Id);
        }

        [HttpPost("edit")]
        [SwaggerOperation("编辑产品", Tags = new[] { "Products" })]
        public async Task EditProduct(ProductInputDto input)
        {
           var entity = await productRepository.GetAsync(p => p.Id == input.Id);
            entity.ProductName = input.ProductName;
            entity.ProductCode = input.ProductCode;
            entity.Description = input.Description;
            await productRepository.UpdateAsync(entity);
        }

        [HttpPost("page")]
        [SwaggerOperation("分页查询产品", Tags = new[] { "Products" })]
        public async Task<PagedResultDto<ProductOutputDto>> ListProduct(PageProductInputDto input)
        {
            var productQuery = await productRepository.GetQueryableAsync();
            var  userQuery =await userRepository.GetQueryableAsync();
            var query = from p in productQuery
                        join u in userQuery on p.CreatorId equals u.Id
                        select new ProductOutputDto()
                        {
                            Id = p.Id,
                            ProductName = p.ProductName,
                            ProductCode = p.ProductCode,
                            Description = p.Description,
                            CreationTime = p.CreationTime,
                            CreatorName = u.Name,
                        };

            query= query.WhereIf(!string.IsNullOrEmpty(input.Filter), p => p.ProductName.Contains(input.Filter));

            var items = query
                .OrderBy(e => e.CreationTime)
                .PageBy(input.SkipCount, input.PageSize)
                .ToList();

            var count = query.Count();

            return new PagedResultDto<ProductOutputDto>()
            {
                Items = items,
                TotalCount = count,
            };
        }
    }
}
